/**
 * Created by huangzx on 2015/7/20.
 */
define('framework/clickbox/GillionClickBoxModule', [
    'angular'
], function (angular) {
    //单选按钮
    function Radio(scope){
        this.scope = scope;
    }

    //单选按钮组
    function RadioGroup() {
        this.radios = [];
    }
    //添加单选按钮
    RadioGroup.prototype.addRadio = function(radio){
        this.radios.push(radio);
    };
    //按钮组赋值
    RadioGroup.prototype.setValue = function(value){
        var radios = this.radios,
            i=0, len=radios.length,
            radio;
        for (; i<len; i++) {
            radio = radios[i];
            if (value != radio.scope.value) radio.scope.selected = false;
        }
    };

    return angular.module('GillionClickBoxModule', []).
        directive('gCheckboxGroup', function (Arrays) {
            return {
                restrict: 'A',
                require: ['gCheckboxGroup', 'ngModel'],
                link: {
                    pre:function (scope, element, attrs, controllers) {
                        var groupController = controllers[0],
                            ngModelController = controllers[1];
                        groupController.setNgModelController(ngModelController);
                    }
                },
                controller: function ($scope) {
                    this.setItemChecked = function (itemValue, checked) {
                        var ngModelController = this.ngModelController,
                            viewValue = ngModelController.$viewValue;
                        if (angular.isUndefined(viewValue)) {
                            viewValue = [];
                            ngModelController.$setViewValue(viewValue);
                        }
                        if (checked && !Arrays.exists(viewValue, itemValue)) {
                            viewValue.push(itemValue);
                        } else if (checked === false) {
                            Arrays.remove(viewValue, itemValue);
                        }
                    };
                    this.setNgModelController = function (ngModelController) {
                        this.ngModelController = ngModelController;
                    };
                }
            }
        }).
        directive('gCheckbox', function (Arrays) {
            return {
                restrict: 'E',
                scope: {
                    display: '@',
                    value: '@',
                    disabled: '@',
                    onBeforeCheck: '&',
                    onCheck: '&'
                },
                require: ['^?gCheckboxGroup', '?ngModel'],
                replace: true,
                template: '<div class="form-clickbox" ng-click="toggleChecked()" ng-selected="selected"><i class="fi"></i><label>{{display}}</label></div>',
                link: function (scope, ele, attrs, controllers) {
                    var groupController = controllers[0],
                        ngModelController = controllers[1],
                        onBeforeCheck = scope.onBeforeCheck || angular.noop,
                        onCheck = scope.onCheck || angular.noop;
                    if (groupController) {
                        if (groupController.ngModelController.$viewValue) {
                            scope.selected = Arrays.exists(groupController.ngModelController.$viewValue, scope.value);
                        } else {
                            groupController.ngModelController.$formatters.push(function (rawValue) {
                                scope.selected = Arrays.exists(rawValue, scope.value);
                                return rawValue;
                            });
                        }
                    } else {
                        if (ngModelController.$viewValue) {
                            scope.selected = (ngModelController.$viewValue == scope.value);
                        } else {
                            ngModelController.$formatters.push(function (rawValue) {
                                if (!scope.value) {
                                    if (rawValue) scope.selected = true;
                                    else scope.selected = false;
                                } else {
                                    scope.selected = (rawValue == scope.value);
                                }
                                return rawValue;
                            });
                        }
                    }

                    scope.toggleChecked = function () {
                        if (attrs.hasOwnProperty('disabled')) return; //禁用控件
                        if (onBeforeCheck() === false) return;
                        scope.selected = !scope.selected;
                        if (groupController) {
                            groupController.setItemChecked(scope.value, scope.selected);
                        } else if (ngModelController) {
                            if (scope.selected) ngModelController.$setViewValue(scope.value||true);
                            else ngModelController.$setViewValue(false);
                        }
                        onCheck({checked:scope.selected});
                    };
                }
            };
        }).
        directive('gRadioGroup', function(){
            return {
                restrict: 'A',
                require: ['gRadioGroup', 'ngModel'],
                link: {
                    pre: function (scope, element, attrs, controllers) {
                        var groupController = controllers[0],
                            ngModelController = controllers[1];
                        groupController.setNgModelController(ngModelController);
                    }
                },
                controller: function ($scope) {
                    this.radioGroup = new RadioGroup();

                    this.setItemChecked = function (itemValue) {
                        var ngModelController = this.ngModelController;
                        ngModelController.$setViewValue(itemValue);
                        this.radioGroup.setValue(itemValue);
                    };

                    this.setNgModelController = function (ngModelController) {
                        this.ngModelController = ngModelController;
                    };
                }

            }
        }).
        directive('gRadio', function(){
            return {
                restrict: 'E',
                scope:{
                    display: '@',
                    value: '@',
                    disabled:'@',
                    onBeforeCheck: '&',
                    onCheck: '&'
                },
                require: ['^?gRadioGroup', '?ngModel'],
                replace: true,
                template: '<div class="form-clickbox" mode="radio" ng-click="toggleChecked()" ng-selected="selected"><i class="fi"></i><label>{{display}}</label></div>',
                link: function(scope, ele, attrs, controllers) {
                    var groupController = controllers[0],
                        ngModelController = controllers[1],
                        onBeforeCheck = scope.onBeforeCheck,
                        onCheck = scope.onCheck;

                    if (groupController) {
                        if (groupController.ngModelController.$viewValue) {
                            scope.selected = (groupController.ngModelController.$viewValue === scope.value);
                        } else {
                            groupController.ngModelController.$formatters.push(function (rawValue) {
                                scope.selected = (rawValue === scope.value);
                                return rawValue;
                            });
                        }
                    } else {
                        if (ngModelController.$viewValue) {
                            scope.selected = (ngModelController.$viewValue === scope.value);
                        } else {
                            ngModelController.$formatters.push(function (rawValue) {
                                scope.selected = (rawValue === scope.value);
                                return rawValue;
                            });
                        }
                    }

                    scope.toggleChecked = function () {
                        if (attrs.hasOwnProperty('disabled')) return; //禁用控件
                        if (onBeforeCheck() === false) return;
                        scope.selected = true;
                        if (groupController) {
                            groupController.setItemChecked(scope.value);
                        } else if (ngModelController) {
                            ngModelController.$setViewValue(scope.value);
                        }
                        onCheck({checked:scope.selected});
                    };

                    var radio = new Radio(scope, scope.value);
                    groupController.radioGroup.addRadio(radio);
                }
            };
        });
});